package com.ywjh.inter.common;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Calendar;

import com.ywjh.inter.utils.StrUtil;

public class Test {
	public static void main(String[] args) {
		String d = "55AA008219000066454E474C013A55C80500E597E1C1223B020000002200000022";
		String head = d.substring(0, 16);
		System.out.println("head==" + head);
		String deviceId = d.substring(16, 32);
		System.out.println("deviceId==" + deviceId);
		String D_SYNC = d.substring(32, 36);
		System.out.println("D_SYNC==" + D_SYNC);
		String TIME = d.substring(36, 46);
		System.out.println("TIME==" + TIME);
		String GLU = d.substring(46, 50);
		System.out.println("GLU==" + GLU);
		String FLAG = d.substring(50, 52);
		System.out.println(FLAG);
		String REG = d.substring(52, 56);
		System.out.println(REG);
		String code = d.substring(56, 60);
		System.out.println(code);
		String TEMP = d.substring(60, 64);
		System.out.println(TEMP);

		String time = getTime(TIME);
		System.out.println(time);
//
		String str = "55AA0082190000664E414449009896801900E1179579510C000000001F00080192";
		System.out.println(str);
		byte D_CHK = 0;
		for (int i = 8 * 2; i < str.length()-2 ; i = i + 2) {
			D_CHK += Integer.valueOf(str.substring(i, i + 2), 16).byteValue();
		}
		D_CHK = (byte) (((byte) ~D_CHK) + 1);
		System.out.println("D_CHK=" + Integer.toHexString(0xFF & D_CHK));

		System.out.println(byteToBitStr((byte) D_CHK));

//String str = "55AA0082190000664E414449009896801900E1179579510C000000001F00080192";
//		
//		//头部数据
//		String headStr=str.substring(0, 8*2);
//		System.out.println("headStr="+headStr);
//		int LEN = Integer.parseUnsignedInt(headStr.substring(10, 12)+headStr.substring(8, 10),  16);
//		System.out.println("LEN="+LEN);
//		
		// 血糖数据
//		String DIDStr = str.substring(8 * 2, 8 * 2 + 8 * 2);
//		System.out.println("DIDStr=" + DIDStr);
//		System.out.println("客户码=" + (char) Byte.parseByte(DIDStr.substring(0, 2), 16)
//				+ (char) Byte.parseByte(DIDStr.substring(2, 4), 16) + (char) Byte.parseByte(DIDStr.substring(4, 6), 16)
//				+ (char) Byte.parseByte(DIDStr.substring(6, 8), 16));
//		System.out.println("机器序列码=" + Integer.parseInt(DIDStr.substring(8, 16), 16));

//		String	D_SYNC= str.substring(8*2+8*2, 8*2+8*2+2*2);
//		System.out.println("D_SYNCStr="+D_SYNC);
//		System.out.println("D_SYNC="+Integer.parseUnsignedInt(D_SYNC.substring(2, 4)+D_SYNC.substring(0, 2),  16));
//		
//		String TIMEStr = str.substring( 8*2+8*2+2*2, 8*2+8*2+2*2+5*2);
//		System.out.println("TIMEStr="+TIMEStr);
//		System.out.println("YEAR="+Integer.parseUnsignedInt(TIMEStr.substring(3, 4)+TIMEStr.substring(0, 2), 16) );
//		System.out.println("MONTH="+Integer.parseUnsignedInt(TIMEStr.substring(2, 3), 16) );
//		byte[] by = new byte[3];
//		by[0]=Integer.valueOf( TIMEStr.substring(4, 6), 16 ).byteValue();
//		by[1]=Integer.valueOf( TIMEStr.substring(6, 8), 16 ).byteValue();
//		by[2]=Integer.valueOf( TIMEStr.substring(8, 10), 16 ).byteValue();
//		System.out.println(getGlu("cc00"));
//
		//System.out.println(getTime("E5A7F4613F"));
		//String hx = time2HX(new Timestamp(System.currentTimeMillis()));
		//System.out.println(hx);
	}

	private static String[] splite(String d, int n) {
		int l = d.length() / n;
		String[] array = new String[l];
		for (int i = 0; i < l; i++) {
			int start = (i) * n;
			int end = start + n;
			array[i] = d.substring(start, end);
		}
		return array;

	}
	
	

	private static float getGlu(String hx) {
		float data = 0.0f;
		// 高低位
		hx = high2low(hx);
		// 转10进制
		float d = Integer.parseUnsignedInt(hx, 16);
		// 血糖单位换算
		data = d / 18;
		BigDecimal b = new BigDecimal(data);
		// 四舍五入
		float round = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
		return round;
	}

	private static String time2HX(Timestamp time) {
		String hx = "";
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time.getTime());
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DATE);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int mi = cal.get(Calendar.MINUTE);
		int ss = cal.get(Calendar.SECOND);
		hx += String.format("%01X", month) + String.format("%03X", year);
		hx = high2low(hx);
		DecimalFormat df5=new DecimalFormat("00000");
		DecimalFormat df6=new DecimalFormat("000000");
		String bs_day = Integer.toBinaryString(day);
		String bs_hh = Integer.toBinaryString(hour);
		String bs_mi = Integer.toBinaryString(mi);
		String bs_ss = Integer.toBinaryString(ss);
		//补位
		bs_day=df5.format(Integer.valueOf(bs_day));
		bs_hh=df6.format(Integer.valueOf(bs_hh));
		bs_mi=df6.format(Integer.valueOf(bs_mi));
		bs_ss=df6.format(Integer.valueOf(bs_ss));
		String b_ddhhmiss = bs_hh.substring(3, 6) + bs_day + bs_mi.substring(1, 6)  + bs_hh.substring(0, 3) + "0"
				+ bs_ss + bs_mi.substring(0, 1);
		hx = hx + String.format("%01X", Integer.parseUnsignedInt(b_ddhhmiss, 2));
		return hx;
	}

	public static void test(int num) {
		String a = "";// 用字符串拼接
		while (num != 0) {// 利用十进制转二进制除2法
			a = num % 2 + a;
			num = num / 2;
		}
		System.out.println(a);
	}

	private static String getTime(String hx) {
		String year = null, month = null, day = null, hour = null, mi = null, ss = null;
		String hx_ym = high2low(hx.substring(0, 4));
		String hxy = hx_ym.substring(1, 4);
		String hxm = hx_ym.substring(0, 1);
		year = Integer.parseInt(hxy, 16) + "";
		month = Integer.parseInt(hxm, 16) + "";
		byte[] bb = hexStr2Bytes(hx.substring(4, hx.length()));
		String binary = "";
		for (byte b : bb) {
			binary += byteToBitStr(b);
		}
		String binaryDay = binary.substring(3, 8);
		day = Integer.parseInt(binaryDay, 2) + "";

		String binaryHour = binary.substring(13, 16) + binary.substring(0, 3);
		hour = Integer.parseInt(binaryHour, 2) + "";
		String binaryMi = binary.substring(23, 24) + binary.substring(8, 13);
		mi = Integer.parseInt(binaryMi, 2) + "";

		String binarySS = binary.substring(18, 23);
		ss = Integer.parseInt(binarySS, 2) + "";
		return year + "-" + month + "-" + day + " " + hour + ":" + mi + ":" + ss;
	}

	public static byte[] hexStr2Bytes(String src) {
		int m = 0, n = 0;
		int l = src.length() / 2;
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			m = i * 2 + 1;
			n = m + 1;
			String s = src.substring(i * 2, m) + src.substring(m, n);
			ret[i] = (byte) Integer.parseUnsignedInt(s, 16);
		}
		return ret;
	}

	public static String high2low(String s) {
		if (s.length() != 4) {
			return null;
		}
		s = s.substring(2, 4) + s.substring(0, 2);
		return s;
	}

	public static String byteToBitStr(byte by) {
		StringBuffer sb = new StringBuffer();
		// 每一位与 000000001按位与运算。保证每一位是 0或者1
		sb.append((by >> 7) & 0x1);
		sb.append((by >> 6) & 0x1);
		sb.append((by >> 5) & 0x1);
		sb.append((by >> 4) & 0x1);
		sb.append((by >> 3) & 0x1);
		sb.append((by >> 2) & 0x1);
		sb.append((by >> 1) & 0x1);
		sb.append((by >> 0) & 0x1);
		return sb.toString();
	}

	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789abcdef".indexOf(c);
		return b;
	}

}
